//
//  DetailViewController.m
//  KeYin
//
//  Created by Sean Chen on 5/26/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "DetailViewController.h"


@implementation DetailViewController

@synthesize periodical;
@synthesize page;
@synthesize detailWebView;
@synthesize detailToolbar;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self setHidesBottomBarWhenPushed:YES];
    }
    return self;
}

- (id)initWithPeriodical:(NSDictionary *)dict {
    self = [self initWithNibName:@"DetailView" bundle:nil];
    if (self) {
        self.periodical = dict;
        page = 1;
    }
    return self;
}

- (void)dealloc
{
    [periodical release];
    [detailWebView release];
    [detailToolbar release];
    
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    NSLog(@"osv = %f", [[[UIDevice currentDevice] systemVersion] floatValue]);
    
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
        UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapFrom:)];
        tapRecognizer.delegate = self;
        [detailWebView addGestureRecognizer:tapRecognizer];
        [tapRecognizer release];
        
        UISwipeGestureRecognizer *swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
        swipeRecognizer.direction = UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight;
        [detailWebView addGestureRecognizer:swipeRecognizer];
        [swipeRecognizer release];
    }
    
    [self reloadWebView];
    
    
    
//    UIImage *image; //original one
//    UIImage *thumb; //compressed one
//    
//    CGFloat kThumbWidth = 320.0;
//    CGFloat kThumbHeight = 640.0;
//    
//    CGSize thumbSize = CGSizeMake(kThumbWidth, kThumbHeight);
//    UIGraphicsBeginImageContext(thumbSize);
//    CGRect thumbRect = CGRectMake(0.0, 0.0, thumbSize.width, thumbSize.height);
//    [image drawInRect:thumbRect];
//    thumb = UIGraphicsGetImageFromCurrentImageContext();
//    UIGraphicsEndImageContext();
    
//    [detailWebView removeFromSuperview];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillDisappear:(BOOL)animated {
    //self.navigationController.navigationBar.barStyle = UIBarStyleBlackOpaque;
    self.navigationController.navigationBar.translucent = NO;
}

- (void)viewWillAppear:(BOOL)animated {
    //self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
    self.navigationController.navigationBar.translucent = YES;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma -

- (void)reloadWebView {
    NSString *src = [NSString stringWithFormat:@"http://www.keyin.cn/ipad/get_magazine.php?periodical_id=%@&page=%d", [periodical objectForKey:@"periodical_id"], page];
    //NSString *src = @"get_magazine.php.jpeg";
    
    NSString *path = [[NSBundle mainBundle] bundlePath];
    NSURL *baseURL = [NSURL fileURLWithPath:path];
    NSString *htmlString = [NSString stringWithFormat:@"<img width=100%% height=100%% src=%@>", src];
    [detailWebView loadHTMLString:htmlString baseURL:baseURL];
    
//    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:src]];
//    [detailWebView loadRequest:request];
}

#pragma -

- (IBAction)pressPrevButton:(id)sender {
    page --;
    [self reloadWebView];
}

- (IBAction)pressNextButton:(id)sender {
    page ++;
    [self reloadWebView];
}

#pragma -

- (void)handleTapFrom:(UITapGestureRecognizer *)recognizer {
    NSLog(@"tap");
    
    _isFullScreen = !_isFullScreen;
    
    [self.navigationController setNavigationBarHidden:_isFullScreen animated:YES];
    
    CGRect rect = detailToolbar.frame;
    if (_isFullScreen) {
        rect.origin.y = 480;
    } else {
        rect.origin.y = 416;
    }
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:UINavigationControllerHideShowBarDuration];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [detailToolbar setFrame:rect];
    [UIView commitAnimations];
}

- (void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
    NSLog(@"swipe");
    if (recognizer.direction == UISwipeGestureRecognizerDirectionLeft) {
        
    } else if (recognizer.direction == UISwipeGestureRecognizerDirectionRight) {
        
    }
}

#pragma -
#pragma UIGestureRecognizerDelegate

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    return YES;
}

@end
